﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using System.ServiceModel;

using PickGold.Callers;
using PickGold.Handler;
using PickGold.Base.Proxies;

namespace PickGold.Base.Remoting
{
	class AsyncInvokerResult : IAsyncInvokerResult, IAsyncResultWrap, IAsyncResult, IDisposable
	{
		public AsyncResultWrap ResultWrap;
		public IMethodInvoker Real;
		public IMethodCallMessage Call;

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="maxStep">默认为一次异步</param>
		public AsyncInvokerResult(int maxStep) : this(maxStep, null, null, null) { }

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="maxStep">默认为一次异步</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		public AsyncInvokerResult(int maxStep, IAsyncResult asyncResult) : this(maxStep, asyncResult, null, null) { }

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="maxStep">默认为一次异步</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public AsyncInvokerResult(int maxStep, AsyncCallback asyncCallback, object asyncState) : this(maxStep, null, asyncCallback, asyncState) { }

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="maxStep">默认为一次异步</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public AsyncInvokerResult(int maxStep, IAsyncResult asyncResult, AsyncCallback asyncCallback, object asyncState)
		{
			this.ResultWrap = new AsyncResultWrap(maxStep, asyncResult, asyncCallback, asyncState);
			this.Real = null;
			this.Call = null;
		}

		IMethodInvoker IAsyncInvokerResult.Real
		{
			set
			{
				this.Real = value;
			}
			get
			{
				return this.Real;
			}
		}

		IMethodCallMessage IAsyncInvokerResult.Call
		{
			set
			{
				this.Call = value;
			}
			get
			{
				return this.Call;
			}
		}

		#region IAsyncResult

		object IAsyncResult.AsyncState
		{
			get
			{
				return this.ResultWrap.AsyncState;
			}
		}

		WaitHandle IAsyncResult.AsyncWaitHandle
		{
			get
			{
				return this.ResultWrap.AsyncWaitHandle;
			}
		}

		bool IAsyncResult.CompletedSynchronously
		{
			get
			{
				return this.ResultWrap.CompletedSynchronously;
			}
		}

		bool IAsyncResult.IsCompleted
		{
			get
			{
				return this.ResultWrap.IsCompleted;
			}
		}

		#endregion

		#region IAsyncResultWrap

		object IAsyncResultWrap.WrapTag
		{
			set
			{
				this.ResultWrap.WrapTag = value;
			}
			get
			{
				return this.ResultWrap.WrapTag;
			}
		}
		object IAsyncResultWrap.WrapReturn
		{
			set
			{
				this.ResultWrap.WrapReturn = value;
			}
			get
			{
				return this.ResultWrap.WrapReturn;
			}
		}
		object[] IAsyncResultWrap.WrapOutput
		{
			set
			{
				this.ResultWrap.WrapOutput = value;
			}
			get
			{
				return this.ResultWrap.WrapOutput;
			}
		}
		Exception IAsyncResultWrap.WrapError
		{
			set
			{
				this.ResultWrap.WrapError = value;
			}
			get
			{
				return this.ResultWrap.WrapError;
			}
		}
		IAsyncResult IAsyncResultWrap.WrapResult
		{
			set
			{
				this.ResultWrap.WrapResult = value;
			}
			get
			{
				return this.ResultWrap.WrapResult;
			}
		}
		AsyncCallback IAsyncResultWrap.WrapCallback
		{
			set
			{
				this.ResultWrap.WrapCallback = value;
			}
			get
			{
				return this.ResultWrap.WrapCallback;
			}
		}
		Delegate IAsyncResultWrap.WrapEnd
		{
			set
			{
				this.ResultWrap.WrapEnd = value;
			}
			get
			{
				return this.ResultWrap.WrapEnd;
			}
		}

		IAsyncResultWrap IAsyncResultWrap.Complete()
		{
			return this.ResultWrap.Complete();
		}

		bool IAsyncResultWrap.WaitOne()
		{
			return this.ResultWrap.WaitOne();
		}

		#endregion

		void IDisposable.Dispose()
		{
			this.ResultWrap.Dispose();
		}
	}

	interface IAsyncInvokerResult : IAsyncResultWrap, IAsyncResult, IDisposable
	{
		IMethodInvoker Real { set; get; }
		IMethodCallMessage Call { set; get; }
	}

	/*




	/// <summary>
	/// 异步包装器，主要用于附加额外的
	/// </summary>
	//[Serializable]
	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.AutoDispatch)]
	class MethodInvokerResult : MethodInvokerResultBase, IMethodInvokerResult, IAsyncInvokerResult, IAsyncResultWrap, IAsyncResult
	{
		private Action<IAsyncInvokerResult> _EndInvoke;

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		public MethodInvokerResult(Action<IAsyncInvokerResult> endInvoke)
			: base(endInvoke)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public MethodInvokerResult(Action<IAsyncInvokerResult> endInvoke, AsyncCallback asyncCallback, object asyncState)
			: base(endInvoke, asyncCallback, asyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public MethodInvokerResult(Action<IAsyncInvokerResult> endInvoke, IAsyncResult asyncResult, AsyncCallback asyncCallback, object asyncState)
			: base(endInvoke, asyncResult, asyncCallback, asyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncException">异步操作错误</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public MethodInvokerResult(Action<IAsyncInvokerResult> endInvoke, Exception asyncException, AsyncCallback asyncCallback, object asyncState)
			: base(endInvoke, asyncException, asyncCallback, asyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		public override Type ResultType
		{
			get
			{
				return typeof(void);
			}
		}

		public override object InvokeResult
		{
			get
			{
				if (this._EndInvoke != null)
					this.EndInvoke();
				return null;
			}
		}

		protected override void EndInvoke()
		{
			if (this._EndInvoke == null)
				return;

			this._EndInvoke(this);
			this._EndInvoke = null;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例并返回中继的回调委托
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		/// <returns>中继调用回调方法</returns>
		public static AsyncCallback CreateAsyncCallback(Action<IAsyncInvokerResult> endInvoke, AsyncCallback asyncCallback, object asyncState)
		{
			return new MethodInvokerResult(endInvoke, asyncCallback, asyncState).Callback;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例并返回中继的回调委托
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		/// <returns>中继调用回调方法</returns>
		public static AsyncCallback CreateAsyncCallback(Action<IAsyncInvokerResult> endInvoke, IAsyncResult asyncResult, AsyncCallback asyncCallback, object asyncState)
		{
			return new MethodInvokerResult(endInvoke, asyncResult, asyncCallback, asyncState).Callback;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例并返回中继的回调委托
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncException">异步操作错误</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		/// <returns>中继调用回调方法</returns>
		public static AsyncCallback CreateAsyncCallback(Action<IAsyncInvokerResult> endInvoke, Exception asyncException, AsyncCallback asyncCallback, object asyncState)
		{
			return new MethodInvokerResult(endInvoke, asyncException, asyncCallback, asyncState).Callback;
		}
	}

	/// <summary>
	/// 异步包装器，主要用于附加额外的
	/// </summary>
	//[Serializable]
	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.AutoDispatch)]
	class MethodInvokerResult<RESULT> : MethodInvokerResultBase, IMethodInvokerResult, IAsyncInvokerResult, IAsyncResultWrap, IAsyncResult
	{
		private Func<IAsyncInvokerResult, RESULT> _EndInvoke;

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		public MethodInvokerResult(Func<IAsyncInvokerResult, RESULT> endInvoke)
			: base(endInvoke)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public MethodInvokerResult(Func<IAsyncInvokerResult, RESULT> endInvoke, AsyncCallback asyncCallback, object asyncState)
			: base(endInvoke, asyncCallback, asyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public MethodInvokerResult(Func<IAsyncInvokerResult, RESULT> endInvoke, IAsyncResult asyncResult, AsyncCallback asyncCallback, object asyncState)
			: base(endInvoke, asyncResult, asyncCallback, asyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncException">异步操作错误</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public MethodInvokerResult(Func<IAsyncInvokerResult, RESULT> endInvoke, Exception asyncException, AsyncCallback asyncCallback, object asyncState)
			: base(endInvoke, asyncException, asyncCallback, asyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		public override Type ResultType
		{
			get
			{
				return typeof(RESULT);
			}
		}

		public override object InvokeResult
		{
			get
			{
				if (this._EndInvoke != null)
					this.EndInvoke();
				return base.InvokeResult;
			}
		}

		protected override void EndInvoke()
		{
			if (this._EndInvoke == null)
				return;

			this.SetInvokeResult(this._EndInvoke(this));
			this._EndInvoke = null;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例并返回中继的回调委托
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		/// <returns>中继调用回调方法</returns>
		public static AsyncCallback CreateAsyncCallback(Func<IAsyncInvokerResult, RESULT> endInvoke, AsyncCallback asyncCallback, object asyncState)
		{
			return new MethodInvokerResult<RESULT>(endInvoke, asyncCallback, asyncState).Callback;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例并返回中继的回调委托
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		/// <returns>中继调用回调方法</returns>
		public static AsyncCallback CreateAsyncCallback(Func<IAsyncInvokerResult, RESULT> endInvoke, IAsyncResult asyncResult, AsyncCallback asyncCallback, object asyncState)
		{
			return new MethodInvokerResult<RESULT>(endInvoke, asyncResult, asyncCallback, asyncState).Callback;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例并返回中继的回调委托
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncException">异步操作错误</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		/// <returns>中继调用回调方法</returns>
		public static AsyncCallback CreateAsyncCallback(Func<IAsyncInvokerResult, RESULT> endInvoke, Exception asyncException, AsyncCallback asyncCallback, object asyncState)
		{
			return new MethodInvokerResult<RESULT>(endInvoke, asyncException, asyncCallback, asyncState).Callback;
		}
	}

	/// <summary>
	/// 异步包装器，主要用于附加额外的
	/// </summary>
	//[Serializable]
	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.AutoDispatch)]
	class MethodInvokerResultStep : MethodInvokerResultBase, IMethodInvokerResult, IAsyncInvokerResult, IAsyncResultWrap, IAsyncResult
	{
		private AsyncResultStepHandler _EndInvoke;

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		public MethodInvokerResultStep(AsyncResultStepHandler endInvoke)
			: base(endInvoke)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public MethodInvokerResultStep(AsyncResultStepHandler endInvoke, AsyncCallback asyncCallback, object asyncState)
			: base(endInvoke, asyncCallback, asyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public MethodInvokerResultStep(AsyncResultStepHandler endInvoke, IAsyncResult asyncResult, AsyncCallback asyncCallback, object asyncState)
			: base(endInvoke, asyncResult, asyncCallback, asyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncException">异步操作错误</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public MethodInvokerResultStep(AsyncResultStepHandler endInvoke, Exception asyncException, AsyncCallback asyncCallback, object asyncState)
			: base(endInvoke, asyncException, asyncCallback, asyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
		}

		public override Type ResultType
		{
			get
			{
				if (this._EndInvoke == null)
					return base.ResultType;

				return typeof(IAsyncResultWrap);
			}
		}

		public override object InvokeResult
		{
			get
			{
				if (this._EndInvoke != null)
					this.EndInvoke();
				return base.InvokeResult;
			}
		}

		protected override void EndInvoke()
		{
			if (this._EndInvoke == null)
				return;

			this.SetInvokeResult(this._EndInvoke(this, this.Callback, this));
			this._EndInvoke = null;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例并返回中继的回调委托
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		/// <returns>中继调用回调方法</returns>
		public static AsyncCallback CreateAsyncCallback(AsyncResultStepHandler endInvoke, AsyncCallback asyncCallback, object asyncState)
		{
			return new MethodInvokerResultStep(endInvoke, asyncCallback, asyncState).Callback;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例并返回中继的回调委托
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		/// <returns>中继调用回调方法</returns>
		public static AsyncCallback CreateAsyncCallback(AsyncResultStepHandler endInvoke, IAsyncResult asyncResult, AsyncCallback asyncCallback, object asyncState)
		{
			return new MethodInvokerResultStep(endInvoke, asyncResult, asyncCallback, asyncState).Callback;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例并返回中继的回调委托
		/// </summary>
		/// <param name="endInvoke">异步结束回调委托</param>
		/// <param name="asyncException">异步操作错误</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		/// <returns>中继调用回调方法</returns>
		public static AsyncCallback CreateAsyncCallback(AsyncResultStepHandler endInvoke, Exception asyncException, AsyncCallback asyncCallback, object asyncState)
		{
			return new MethodInvokerResultStep(endInvoke, asyncException, asyncCallback, asyncState).Callback;
		}
	}

	/// <summary>
	/// 异步包装器
	/// </summary>
	abstract class MethodInvokerResultBase : IMethodInvokerResult, IAsyncInvokerResult, IAsyncResultWrap, IAsyncResult
	{
		private static readonly object LOCK = new object();

		private object _Invoker;
		private IMethodCallMessage _MethodCallMessage;
		private OperationContext _OperationContext;
		private int _Handled;
		private object _InvokeResult;
		private IAsyncResult _AsyncResult;
		private object _AsyncResultState;
		private object _AsyncState;
		private AsyncCallback _AsyncCallback;
		private AsyncCallback _StepCallback;
		private Exception _AsyncException;
		private Delegate _EndInvoke;
		private ManualResetEvent _WaitHandle;//DoubleWaitHandle

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">返回异步操作回调</param>
		protected MethodInvokerResultBase(Delegate endInvoke)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
			this._AsyncResult = null;
			this._AsyncCallback = null;
			this._AsyncState = null;
			this._AsyncException = null;
			this._Invoker = CMessage.ThreadHandler;
			this._MethodCallMessage = CMessage.ThreadMessage as IMethodCallMessage;
			this._OperationContext = OperationContext.Current;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">返回异步操作回调</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="anyncState">附加的异步操作状态的用户定义对象</param>
		protected MethodInvokerResultBase(Delegate endInvoke, AsyncCallback asyncCallback, object anyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
			this._AsyncResult = null;
			this._AsyncCallback = asyncCallback;
			this._AsyncState = anyncState;
			this._AsyncException = null;
			this._Invoker = CMessage.ThreadHandler;
			this._MethodCallMessage = CMessage.ThreadMessage as IMethodCallMessage;
			this._OperationContext = OperationContext.Current;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">返回异步操作回调</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="anyncState">附加的异步操作状态的用户定义对象</param>
		protected MethodInvokerResultBase(Delegate endInvoke, IAsyncResult asyncResult, AsyncCallback asyncCallback, object anyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
			this._AsyncResult = asyncResult;
			this._AsyncCallback = asyncCallback;
			this._AsyncState = anyncState;
			this._Invoker = CMessage.ThreadHandler;
			this._MethodCallMessage = CMessage.ThreadMessage as IMethodCallMessage;
			this._OperationContext = OperationContext.Current;
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="endInvoke">返回异步操作回调</param>
		/// <param name="asyncException">异步操作错误</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="anyncState">附加的异步操作状态的用户定义对象</param>
		protected MethodInvokerResultBase(Delegate endInvoke, Exception asyncException, AsyncCallback asyncCallback, object anyncState)
		{
			if (endInvoke == null)
				throw new ArgumentNullException();

			this._EndInvoke = endInvoke;
			this._AsyncResult = null;
			this._AsyncCallback = asyncCallback;
			this._AsyncState = anyncState;
			this._AsyncException = asyncException;
			this._Invoker = CMessage.ThreadHandler;
			this._MethodCallMessage = CMessage.ThreadMessage as IMethodCallMessage;
			this._OperationContext = OperationContext.Current;
		}

		~MethodInvokerResultBase()
		{
			this.Dispose(true);
		}

		/// <summary>
		/// 回调是否已调用，此状态是否已过期
		/// </summary>
		public bool Handled
		{
			get
			{
				return (this._Handled == int.MaxValue);
			}
		}

		/// <summary>
		/// 被包装的异步操作状态对象
		/// </summary>
		public IAsyncResult WrapResult
		{
			set
			{
				//if (value != null && (value != this || this._AsyncResult == null))
				//if (this._AsyncResult == null && value != null && value != this)
				if (value != null && value != this)
					this._AsyncResult = value;
			}
			get
			{
				//if (this._AsyncResult == this)
				//	return null;

				return this._AsyncResult;
			}
		}

		/// <summary>
		/// 附加的异步操作状态的用户定义对象
		/// </summary>
		public object AsyncState
		{
			get
			{
				if (this._AsyncState == null && this._AsyncResult != null)
					return this._AsyncResult.AsyncState;

				return this._AsyncState;
			}
		}

		/// <summary>
		/// 获取异步操作错误
		/// </summary>
		public Exception WrapError
		{
			get
			{
				return this._AsyncException;
			}
		}

		#region IAsyncResult

		object IAsyncResult.AsyncState
		{
			get
			{
				//if (this._AsyncResult == null)
				//    return this._AsyncState;

				//return this._AsyncResult.AsyncState;
				if (this._AsyncState != null)
					return this._AsyncState;

				if (this._AsyncResult != null)
					return this._AsyncResult.AsyncState;

				return null;
			}
		}

		WaitHandle IAsyncResult.AsyncWaitHandle
		{
			get
			{
				lock (LOCK)
				{
					if (this._WaitHandle == null)
						this._WaitHandle = new ManualResetEvent(this._Handled == 0 ? DoubleWaitHandle.PAUSE : DoubleWaitHandle.CONTINUE);// new DoubleWaitHandle(this._AsyncResult == null ? null : this._AsyncResult.AsyncWaitHandle, false);
				}
				return this._WaitHandle;
			}
		}

		bool IAsyncResult.CompletedSynchronously
		{
			get
			{
				//if (this._IsCalled != 0)
				//{
				//    if (this._WaitHandle == null)
				//        return true;

				//    return false;
				//}

				if (this._AsyncResult != null)
					return this._AsyncResult.CompletedSynchronously;

				if (this._WaitHandle == null)
					return true;

				return false;
			}
		}

		bool IAsyncResult.IsCompleted
		{
			get
			{
				if (this._Handled == 0)
					return false;

				if (this._AsyncResult != null)
					return this._AsyncResult.IsCompleted;

				//if (this._WaitHandle == null)
				//    return true;

				//return false;
				return true;
			}
		}

		#endregion

		object IMethodInvokerResult.Invoker
		{
			set
			{
				if (this._Invoker == null && value != null)
					this._Invoker = value;
			}
			get
			{
				return this._Invoker;
			}
		}

		IMethodCallMessage IMethodInvokerResult.MethodCallMessage
		{
			set
			{
				if (this._MethodCallMessage == null && value != null)
					this._MethodCallMessage = value;
			}
			get
			{
				return this._MethodCallMessage;
			}
		}

		OperationContext IMethodInvokerResult.Context
		{
			set
			{
				if (this._OperationContext == null && value != null)
					this._OperationContext = value;
			}
			get
			{
				return this._OperationContext;
			}
		}

		Delegate IMethodInvokerResult.EndInvokeHandler
		{
			get
			{
				return this._EndInvoke;
			}
		}

		object IAsyncResultWrap.WrapState
		{
			get
			{
				return this.AsyncState;
			}
		}

		/// <summary>
		/// 结果类型
		/// </summary>
		public virtual Type ResultType
		{
			get
			{
				return this._EndInvoke.Method.ReturnType;
			}
		}

		/// <summary>
		/// 异步调用结果
		/// </summary>
		public virtual object InvokeResult
		{
			get
			{
				return this._InvokeResult;
			}
		}

		/// <summary>
		/// 设置执行结果
		/// </summary>
		/// <param name="value">执行结果</param>
		protected void SetInvokeResult(object value)
		{
			this._InvokeResult = value;
		}

		void IMethodInvokerResult.EndInvoke()
		{
			this.EndInvoke();
		}

		/// <summary>
		/// 执行异步回调
		/// </summary>
		/// <param name="asyncResult">异步操作状态</param>
		protected abstract void EndInvoke();

		/// <summary>
		/// 获取原引用在相应异步操作完成时调用的方法
		/// </summary>
		public AsyncCallback InvokeCallback()
		{
			return this._AsyncCallback;
		}

		/// <summary>
		/// 获取或调用原引用在相应异步操作完成时调用的方法
		/// </summary>
		/// <param name="exception">异步操作异常，如果操作异常不为空则调用否则返回原引用在相应异步操作完成时调用的方法</param>
		public AsyncCallback InvokeCallback(Exception exception)
		{
			if (exception == null)
				return this._AsyncCallback;

			this._AsyncException = exception;
			this.Callback(this);
			return this.Callback;
		}

		/// <summary>
		/// 获取或调用原引用在相应异步操作完成时调用的方法
		/// </summary>
		/// <param name="asyncResult">异步操作状态，如果操作状态不为空则调用否则返回原引用在相应异步操作完成时调用的方法</param>
		public AsyncCallback InvokeCallback(IAsyncResult asyncResult)
		{
			if (asyncResult == null)
				return this._AsyncCallback;

			this.Callback(asyncResult);
			return this.Callback;
		}

		/// <summary>
		/// 中继调用回调方法
		/// </summary>
		/// <param name="result">异步调用状态</param>
		protected void Callback(IAsyncResult result)
		{
			if (result != null && result != this)
				this._AsyncResult = result;
			if (this._StepCallback != null && this._AsyncException == null)
			{
				var cb = this._StepCallback;
				this._StepCallback = null;
				try
				{
					if (this._AsyncCallback != null)
					{
						cb(result);
						return;
					}

					this._AsyncCallback = cb;
				}
				catch (Exception x)
				{
					this._AsyncException = x;
				}
			}
			lock (LOCK)
			{
				if (this._Handled != 0)
					return;

				this._Handled = int.MaxValue;
			}
			//if (Interlocked.Exchange(ref this._IsCalled, int.MaxValue) != 0)//if (this._IsCalled == int.MaxValue)//
			//	return;

			if (this._WaitHandle != null)
				this._WaitHandle.Set();
			if (this._AsyncCallback != null)
				this._AsyncCallback(this);
		}

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例并返回中继的回调委托
		/// </summary>
		/// <returns>中继调用回调方法</returns>
		public AsyncCallback GetInvokeCallback()
		{
			return this.Callback;
		}

		/// <summary>
		/// 返回中继的回调委托
		/// </summary>
		/// <param name="stepCallback">多步异步操作的中间回调</param>
		/// <returns>中继调用回调方法</returns>
		public AsyncCallback GetInvokeCallback(AsyncCallback stepCallback)
		{
			if (this._AsyncCallback == null)
				this._AsyncCallback = stepCallback;
			else if (this._AsyncCallback != stepCallback)
				this._StepCallback = stepCallback;
			return this.Callback;
		}

		/// <summary>
		/// 设置异步异常。
		/// </summary>
		/// <param name="exception">异步异常。</param>
		/// <returns>如果该操作成功，则为 true；否则，为 false。</returns>
		public bool SetException(Exception exception)
		{
			if (this._AsyncException != null)
				return false;

			if (exception == null)
				return false;

			this._AsyncException = exception;
			return true;
		}

		/// <summary>
		/// 执行与释放或重置非托管资源相关的应用程序定义的任务。
		/// </summary>
		public void Dispose()
		{
			this.Dispose(false);
		}

		/// <summary>
		/// 执行与释放或重置非托管资源相关的应用程序定义的任务。
		/// </summary>
		/// <param name="disposing">是否是GC在销毁资源。</param>
		protected virtual void Dispose(bool disposing)
		{
			if (this._WaitHandle != null)
				this._WaitHandle.Close();
			this._WaitHandle = null;
		}
	}

	interface IMethodInvokerResult : IAsyncInvokerResult, IAsyncResultWrap, IAsyncResult, IDisposable
	{
		/// <summary>
		/// 当前调用的实例
		/// </summary>
		object Invoker { set;  get; }
		Delegate EndInvokeHandler { get; }
		void EndInvoke();
		/// <summary>
		/// 当前调用消息
		/// </summary>
		IMethodCallMessage MethodCallMessage { set; get; }
		/// <summary>
		/// 执行方法的上下文
		/// </summary>
		OperationContext Context { set; get; }
	}

	/// <summary>
	/// 异步操作的状态操作
	/// </summary>
	public interface IAsyncInvokerResult : IAsyncResultWrap, IAsyncResult, IDisposable
	{
		/// <summary>
		/// 调用结果
		/// </summary>
		object InvokeResult { get; }
		/// <summary>
		/// 结果类型
		/// </summary>
		Type ResultType { get; }
		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例并返回中继的回调委托
		/// </summary>
		/// <returns>中继调用回调方法</returns>
		AsyncCallback GetInvokeCallback();
		/// <summary>
		/// 返回中继的回调委托
		/// </summary>
		/// <param name="stepCallback">多步异步操作的中间回调</param>
		/// <returns>中继调用回调方法</returns>
		AsyncCallback GetInvokeCallback(AsyncCallback stepCallback);
		/// <summary>
		/// 设置异步异常。
		/// </summary>
		/// <param name="exception">异步异常。</param>
		/// <returns>如果该操作成功，则为 true；否则，为 false。</returns>
		bool SetException(Exception exception);
	}

	*/
}
